package com.weilive.pay;

import org.springframework.context.ApplicationContext;

import com.weilive.core.utils.FileUtils;
import com.weilive.core.utils.PropertiesFile;

/**
 * 服务器常量配置
 * 
 * @author houzhiwei
 * 
 */
public final class ServerContext {
	private static final String PROPERTIES_FILE = "system.properties";
	private static final int WORKING_ENV = 1;
	// spring容器管理
	private static ApplicationContext context = null;
	// 工作目录
	private static String workPath;
	// 资源路径
	private static String resourcePath = "/SResource/";
	// js资源路径
	private static String scriptPath = "/SResource/javascript/";
	private static long sessionTimeOut = 60 * 60 * 1000L;

	private static int env = 0;

	private ServerContext() {
	}

	public static Object getBean(String beanName) {
		return (Object) context.getBean(beanName);
	}

	public static Object getBean(Class<?> claze) {
		String className = claze.getSimpleName();
		className = className.substring(0, 1).toLowerCase() + className.substring(1);

		return getBean(className);
	}

	private static void setWorkPath(String workPath) {
		ServerContext.workPath = workPath;
	}

	public static void setContext(ApplicationContext context) {
		ServerContext.context = context;
	}

	public static String getScriptPath() {
		return scriptPath;
	}

	public static String getResourcePath() {
		return resourcePath;
	}

	public static long getSessionTimeOut() {
		return sessionTimeOut;
	}

	public static int getEnv() {
		return env;
	}

	public static void setEnv(int env) {
		ServerContext.env = env;
	}

	public static boolean isWorkingEnv() {
		return env == WORKING_ENV;
	}

	/**
	 * 加载资源
	 * 
	 */
	public static void loadData(String workPath) {
		// 设置工作目录
		setWorkPath(workPath);
		// 从system.properties文件中加载资源
		reloadData();
	}

	public static void reloadData() {
		PropertiesFile properties = new PropertiesFile(FileUtils.getResourcePath(PROPERTIES_FILE));
		resourcePath = workPath + properties.get("bean.resourcePath", "/SResource/");
		scriptPath = workPath + properties.get("bean.scriptPath", "/SResource/javascript/");
		sessionTimeOut = properties.getLong("cache.timeout", 60) * 60 * 1000L;
		env = properties.getInt("ENV", 3);
	}

}
